package com.RLFZ.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.RLFZ.entity.User;
import com.RLFZ.vo.DataView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 86166
 */
@Controller
public class LoginController {
//    @Autowired
//    private UserService userService;
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * 验证码
     * 验证码功能位于cn.hutool.captcha包中，核心接口为ICaptcha，此接口定义了以下方法：
     * createCode 创建验证码，实现类需同时生成随机验证码字符串和验证码图片
     * getCode 获取验证码的文字内容
     * verify 验证验证码是否正确，建议忽略大小写
     * write 将验证码写出到目标流中
     * 其中write方法只有一个OutputStream，ICaptcha实现类可以根据这个方法封装写出到文件等方法。
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("/login/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,10);
        session.setAttribute("code",lineCaptcha.getCode());
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            // 将验证码写入到文件输出流中
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     *
     */
    @RequestMapping("/login/login")
    @ResponseBody
    public DataView login(String username,String password,String code,HttpSession session){
        DataView dataView=new DataView();
        //1.首先判断验证码是否正确
        String sessionCode = (String)session.getAttribute("code");
        if (code!=null && sessionCode.equals(code)){
            //2登录逻辑 session
            //  User user=userService.login(username,password);
            //  shiro登录
            User user=null;
                Subject subject= SecurityUtils.getSubject();
                UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            try{
                subject.login(token);
                user=(User) subject.getPrincipal();
            }catch (UnknownAccountException e){
                dataView.setCode(100);
                dataView.setMsg("用户不存在,登录失败");
                return dataView;
            }catch (IncorrectCredentialsException e){
                dataView.setCode(100);
                dataView.setMsg("用户密码错误,登录失败");
                return dataView;
            }catch (Exception e){
                dataView.setCode(100);
                dataView.setMsg("未知错误,登录失败");
                return dataView;
            }
            //3判断
            if (user!=null){
                dataView.setCode(200);
                dataView.setMsg("登录成功");
//                4放入session
                session.setAttribute("user",user);
                return dataView;
            }else {
                dataView.setMsg("未知错误，登录失败");
                dataView.setCode(100);
                return dataView;
            }
        }
        dataView.setMsg("验证码错误");
        dataView.setCode(100);
        return dataView;
    }
}
